package com.ihr360.expr.core;

import com.ihr360.expr.ExprException;
import com.ihr360.expr.ExprString;
import com.ihr360.expr.context.IEvaluationContext;

public abstract class AbstractComparisonOperator extends AbstractBinaryOperator
{
    public AbstractComparisonOperator(ExprType type, Expr lhs, Expr rhs) {
        super(type, lhs, rhs);
    }
    protected double compare(IEvaluationContext context) throws ExprException {
        Expr l = eval(lhs, context);
        Expr r = eval(rhs, context);

        if (l instanceof ExprString || r instanceof ExprString) {
            return l.toString().compareTo(r.toString());
        }

        if (l instanceof ExprNumber && r instanceof ExprNumber) {
            return (((ExprNumber) l).doubleValue() - ((ExprNumber) r)
                    .doubleValue());
        }

        return 0;
    }
}